﻿@model Inscriptionem.Models.Blog.Base.BaseViewModel

<ul>
@if (Model.Pages.Any())
{
<text>
<li>
    <h2>Страницы</h2>
	<ul>
        @foreach (var page in Model.Pages)
        {
		<text>
        <li>
            <a href="/@Model.Language/@Model.Section/Page/@page.ID" title="@page.Title">@page.Title</a>
        </li>
        </text>
        }
    </ul>
</li>
</text>
}

@if (Model.Categories.Any())
{
<text>
<li class="categories">
    <h2>Категории</h2>
    <ul>
        @foreach (var category in Model.Categories.OrderBy(c => c.Name))
        { 
        <text>
        <li class="cat-item">
            <a href="/@Model.Language/@Model.Section/Category/@category.ID" title="@category.Name">@category.Name</a>
        </li>
        </text>
        }
    </ul>
</li>
<li>
    <h2>Новые посты</h2>
	<ul>
        @foreach (var post in Model.Categories.SelectMany(c => c.PostCategories).Select(pc => pc.Post).Where(p => p.IsPublished).Distinct().OrderByDescending(c => c.CreatedDate).Take(10))
        { 
        <text>
		<li>
            <a href="/@Model.Language/@Model.Section/Post/@post.ID" title="@post.Title">@post.Title</a>
        </li>
        </text>
        }
    </ul>
</li>
</text>
}

@{
    bool commentsExists = Model.Categories.Any() || Model.Pages.Any();
    if (commentsExists)
    {
        var comments = Model.Categories.SelectMany(c => c.PostCategories).Select(pc => pc.Post).SelectMany(p => p.Comments)
            .Union(Model.Pages.SelectMany(p => p.Comments)).Where(c => (c.IsPublished && !c.IsDeleted) || (Inscriptionem.Authorization.Member.Current.ID != 0 && (!c.IsDeleted && c.Member.ID == Inscriptionem.Authorization.Member.Current.ID)))
            .Distinct().OrderByDescending(c => c.CreatedDate);
        
        if (comments.Any())
        { 
        <text>
        <li>
            <h2>Новые комментарии</h2>
            <ul>
                @foreach (var comment in comments)
                {
                    var link = "/" + Model.Language + "/" + Model.Section + "/" + (comment.Page != null ? "Page/" + comment.Page.ID : "Post/" + comment.Post.ID) + "/#Comment-" + comment.ID;
                <text>
                <li>
                    <a href="@link">
                        <strong>@(comment.Member.MemberSettings.IsRealNameVisible ? comment.Member.FirstName + " " + comment.Member.LastName : comment.Member.NickName)</strong>
                    </a>: @(comment.Content.Length > 45 ? comment.Content.Substring(0, 45): comment.Content)
                    <a href="@link"><b> »</b></a>
                </li>
                </text>
                }
            </ul>
        </li>	
        </text>
        } 
    }
}

@if (Model.Tags.Any())
{
<text>
<li>
    <h2>Теги</h2>
    <!-- TODO: Take up to 15 random tags -->
    <ul>
        @foreach (var tag in Model.Tags)
        {
            var link = "/" + Model.Language + "/" + Model.Section + "/Tag/" + tag.ID;
        <text>
        <li>
            <a href="@link">@tag.Name</a>
        </li>
        </text>
        }
    </ul>
</li>
</text>
}		
</ul>